package com.example.demo.config;

import com.example.demo.interceptor.FirstInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置类
 * @author wch
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    //关键，将拦截器作为bean写入配置中
    @Bean
    public FirstInterceptor myInterceptor(){
        return new FirstInterceptor();
    }

    /**
     * 注册配置的拦截器
     * addPathPatterns 表示拦截哪些请求地址
     * excludePathPatterns 表示不拦截的请求地址
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                .addPathPatterns("/user/list");
//                .excludePathPatterns("/user/list");
    }
}
